<template>
  <el-form :model="yonghu" label-position="right" label-width="auto">
    <el-row :gutter="10">
      <el-col :span="12">
        <el-form-item label="账号" prop="name">
          <el-input v-model="yonghu.yhzh" placeholder="请输入账号" clearable/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="密码" prop="author">
          <el-input v-model="yonghu.yhzh" placeholder="请输入密码" clearable/>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row :gutter="10">
      <el-col :span="12">
        <el-form-item label="用户身份" prop="date">
          <el-radio-group v-model="yonghu.yhsf">
            <el-radio value="0" size="small">管理员</el-radio>
            <el-radio value="3" size="small">普通用户</el-radio>
            <el-radio value="6" size="small">商家</el-radio>
            <el-radio value="9" size="small">骑手</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="昵称" prop="press">
          <el-input v-model="yonghu.yhnc" placeholder="请输入昵称" clearable/>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row :gutter="10">
      <el-col :span="12">
        <el-form-item label="联系方式" prop="price">
          <el-input v-model="yonghu.phone" placeholder="请输入联系方式" clearable/>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>

  <el-row>
    <el-col>
      <div style="text-align: center;">
        <el-button type="primary" @click="doSave">保存</el-button>
        <el-button @click="doCancel">取消</el-button>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits([
  "closeDialog",	// 关闭 dialog
  "flushPage"		// 刷新页面
]);

const yonghu = ref({
  yhzh: "",
  pwd: "",
  yhsf:"",
  yhnc: "",
  phone: "",
})

const handleUpdateData = (obj) => {
  yonghu.value = obj;
}

// 暴露给父组件使用的方法
defineExpose({
  handleUpdateData,
})

// 取消
const doCancel = () => {
  emits("closeDialog");
}

// 更新
const doSave = async () => {
  const {data} = await axios.put('/api/shenfen', yonghu.value);
  if (data.code === 200) {
    emits("flushPage");				// 触发事件： 刷新页面
    emits("closeDialog");			// 触发事件：关闭 dialog
    ElMessage.success("操作成功！");
  } else {
    ElMessage.error("操作失败！");
  }
}
</script>

<style scoped>

</style>